--[[

GUI Code for configuration.

]]

--[[
Button Functions
]]
function LCB_GUI_SaveButtonClick()
	-- Actual program status
	LCB_Config.enabled		= LCB_GUI_Enable:GetChecked();

	-- Run on Login?
	LCB_Config.login		= LCB_GUI_Login:GetChecked();

	-- Run when leaving combat?
	LCB_Config.combat		= LCB_GUI_Combat:GetChecked();

	-- Individual items
	LCB_Config.HealthSlot		= _G["LCB_HealthSlot"]:GetText();
	LCB_Config.ManaSlot		= _G["LCB_ManaSlot"]:GetText();
	LCB_Config.BandageSlot		= _G["LCB_BandageSlot"]:GetText();
	LCB_Config.HealthPotionSlot	= _G["LCB_HealthPotionSlot"]:GetText();
	LCB_Config.ManaPotionSlot	= _G["LCB_ManaPotionSlot"]:GetText();
end

function LCB_GUI_DefaultsButtonClick()
	-- Actual program status
	LCB_Config.enabled		= 1;

	-- Run on Login?
	LCB_Config.login		= 1;

	-- Run when leaving combat?
	LCB_Config.combat		= 0;

	-- Individual items
	LCB_Config.HealthSlot		= "37";
	LCB_Config.ManaSlot		= "38";
	LCB_Config.BandageSlot		= "39";
	LCB_Config.HealthPotionSlot	= "40";
	LCB_Config.ManaPotionSlot	= "41";
	
	LCB_GUI_FillData();
end

function LCB_GUI_DefaultsNMButtonClick()
	-- Actual program status
	LCB_Config.enabled		= 1;

	-- Run on Login?
	LCB_Config.login		= 1;

	-- Run when leaving combat?
	LCB_Config.combat		= 0;

	-- Individual items
	LCB_Config.HealthSlot		= "37";
	LCB_Config.ManaSlot		= "0";
	LCB_Config.BandageSlot		= "38";
	LCB_Config.HealthPotionSlot	= "39";
	LCB_Config.ManaPotionSlot	= "0";
	
	LCB_GUI_FillData();
end

--[[

GUI Control Function
]]
local function LCB_GUI_SetEditBoxDetails(myFrame, myText)
	-- Set the Edit Box Size
	myFrame:SetSize(50, 32);
	myFrame:SetMaxBytes(6);
	
	-- Disable Frame auto-focus
	myFrame:SetAutoFocus(false);

	-- Show the frame
	myFrame:Show();
	myFrame:ClearFocus();

	-- Set "blank" text
	myFrame:SetText("-");
end

-- Function: Fill the data in the GUI config screen
function LCB_GUI_FillData()
	-- Actual program status
	LCB_GUI_Enable:SetChecked(LCB_Config.enabled);

	-- Run on Login?
	LCB_GUI_Login:SetChecked(LCB_Config.login);

	-- Run when leaving combat?
	LCB_GUI_Combat:SetChecked(LCB_Config.combat);

	-- Individual items
	_G["LCB_HealthSlot"]:SetText(LCB_Config.HealthSlot);
	_G["LCB_ManaSlot"]:SetText(LCB_Config.ManaSlot);
	_G["LCB_BandageSlot"]:SetText(LCB_Config.BandageSlot);
	_G["LCB_HealthPotionSlot"]:SetText(LCB_Config.HealthPotionSlot);
	_G["LCB_ManaPotionSlot"]:SetText(LCB_Config.ManaPotionSlot);
end

-- Function: Add a description textfield to the left of another object
local function LCB_GUI_AddDescLeft(myFrame, myText)
	local leftDesc = myFrame:CreateFontString("leftDesc");
	leftDesc:ClearAllPoints();
	leftDesc:SetFontObject(GameFontNormal);
	leftDesc:SetJustifyH("RIGHT");
	leftDesc:SetJustifyV("TOP");
	leftDesc:SetPoint("TOPRIGHT", myFrame, "TOPLEFT", -10, -9);
	leftDesc:SetText(myText);
	leftDesc:Show();
end

-- Function: Setup the main GUI window.
function LCB_RegConfig()
	-- Create main frame for information text
	LCB_GUI	= CreateFrame("FRAME", "LCB_GUI");
	LCB_GUI.name	= LCB_DESC;
	LCB_GUI.refresh	= LCB_GUI_UpdateValues;
	InterfaceOptions_AddCategory(LCB_GUI);

	-- Options Header
	LCB_GUI_Header = LCB_GUI:CreateFontString(nil, "OVERLAY");
	LCB_GUI_Header:SetFontObject(GameFontNormalLarge);
	LCB_GUI_Header:SetJustifyH("LEFT");
	LCB_GUI_Header:SetJustifyV("TOP");
	LCB_GUI_Header:ClearAllPoints();
	LCB_GUI_Header:SetPoint("TOPLEFT", 10, -10);
	LCB_GUI_Header:SetText(LCB_DESC);

	-- Version
	LCB_GUI_Version = LCB_GUI:CreateFontString(nil, "OVERLAY")
	LCB_GUI_Version:SetFontObject(GameFontNormalLarge)
	LCB_GUI_Version:SetJustifyH("LEFT")
	LCB_GUI_Version:SetJustifyV("TOP")
	LCB_GUI_Version:ClearAllPoints()
	LCB_GUI_Version:SetPoint("TOPLEFT", LCB_GUI_Header,"BOTTOMLEFT", 0, 0)
	LCB_GUI_Version:SetText("Version "..LCB_VERSION_NUM)

	-- Scroll Frame + ScrollChild
	LCB_GUI_OPTIONS = CreateFrame("ScrollFrame", "LCB_GUI_OPTIONS", LCB_GUI, "UIPanelScrollFrameTemplate");
	LCB_GUI_OPTIONS_SC = CreateFrame("Frame", "TestScrollChild", LCB_GUI_OPTIONS);
	LCB_GUI_OPTIONS:SetScrollChild(LCB_GUI_OPTIONS_SC);
	LCB_GUI_OPTIONS:SetPoint("TOPLEFT", LCB_GUI, "TOPLEFT", 5, -45);
	LCB_GUI_OPTIONS_SC:SetPoint("TOPLEFT", "LCB_GUI_OPTIONS", "TOPLEFT", 0, 0);
	LCB_GUI_OPTIONS:SetWidth(580);
	LCB_GUI_OPTIONS:SetHeight(510);
	LCB_GUI_OPTIONS_SC:SetWidth(580);
	LCB_GUI_OPTIONS_SC:SetHeight(500);
	LCB_GUI_OPTIONS:SetHorizontalScroll(0);
	LCB_GUI_OPTIONS:SetVerticalScroll(0);
	LCB_GUI_OPTIONS:EnableMouse(true);
	LCB_GUI_OPTIONS:Show();
	LCB_GUI_OPTIONS_SC:Show();

--[[
	Need to put a header in here somehow
	
	Options below are for the entire addon.
	
	]]
	-- Addon Enabled
	LCB_GUI_Enable = CreateFrame("CheckButton", "LCB_GUI_Enable", LCB_GUI_OPTIONS_SC, "UICheckButtonTemplate");
	LCB_GUI_Enable:ClearAllPoints();
	LCB_GUI_Enable:SetChecked(LCB_Config.enabled);
	LCB_GUI_EnableText:SetText("Enable Button Generation");
	LCB_GUI_Enable:SetPoint("TOPLEFT", LCB_GUI_OPTIONS_SC, "TOPLEFT", 0, -30);

	-- On Login Button
	LCB_GUI_Login = CreateFrame("CheckButton", "LCB_GUI_Login", LCB_GUI_OPTIONS_SC, "UICheckButtonTemplate");
	LCB_GUI_Login:ClearAllPoints();
	LCB_GUI_Login:SetChecked(LCB_Config.enabled);
	LCB_GUI_LoginText:SetText("Run on Login");
	LCB_GUI_Login:SetPoint("TOPLEFT", LCB_GUI_Enable, "TOPLEFT", 0, -30);

	-- When leaving combat Button
	LCB_GUI_Combat = CreateFrame("CheckButton", "LCB_GUI_Combat", LCB_GUI_OPTIONS_SC, "UICheckButtonTemplate");
	LCB_GUI_Combat:ClearAllPoints();
	LCB_GUI_Combat:SetChecked(LCB_Config.enabled);
	LCB_GUI_CombatText:SetText("Run when leaving combat");
	LCB_GUI_Combat:SetPoint("TOPLEFT", LCB_GUI_Login, "TOPLEFT", 0, -30);

	
--[[
	Action Bar Slot Locations

	-- ActionBar page 4 (Right ActionBar 2): slots 37 to 48  (top -> bottom)
]]
	
	-- Where to put the Health Regen Button
	local LCB_HealthSlot = CreateFrame("EditBox", "LCB_HealthSlot", LCB_GUI_OPTIONS_SC, "InputBoxTemplate");
	LCB_HealthSlot:SetPoint("TOPLEFT", LCB_GUI_Combat, "TOPLEFT", 250, -50);
	LCB_GUI_SetEditBoxDetails(LCB_HealthSlot);
	LCB_GUI_AddDescLeft(LCB_HealthSlot, "Health Regen Button Slot -");

	-- Where to put the Mana Regen Button
	local LCB_ManaSlot = CreateFrame("EditBox", "LCB_ManaSlot", LCB_GUI_OPTIONS_SC, "InputBoxTemplate");
	LCB_ManaSlot:SetPoint("TOPLEFT", LCB_HealthSlot, "TOPLEFT", 0, -30);
	LCB_GUI_SetEditBoxDetails(LCB_ManaSlot);
	LCB_GUI_AddDescLeft(LCB_ManaSlot, "Mana Regen Button Slot -");

	-- Where to put the Bandages Button
	local LCB_BandageSlot = CreateFrame("EditBox", "LCB_BandageSlot", LCB_GUI_OPTIONS_SC, "InputBoxTemplate");
	LCB_BandageSlot:SetPoint("TOPLEFT", LCB_ManaSlot, "TOPLEFT", 0, -30);
	LCB_GUI_SetEditBoxDetails(LCB_BandageSlot);
	LCB_GUI_AddDescLeft(LCB_BandageSlot, "Bandage Button Slot -");

	-- Where to put the Health Potion Button
	local LCB_HealthPotionSlot = CreateFrame("EditBox", "LCB_HealthPotionSlot", LCB_GUI_OPTIONS_SC, "InputBoxTemplate");
	LCB_HealthPotionSlot:SetPoint("TOPLEFT", LCB_BandageSlot, "TOPLEFT", 0, -30);
	LCB_GUI_SetEditBoxDetails(LCB_HealthPotionSlot);
	LCB_GUI_AddDescLeft(LCB_HealthPotionSlot, "Health Potion Button Slot -");

	-- Where to put the Mana Potion Button
	local LCB_ManaPotionSlot = CreateFrame("EditBox", "LCB_ManaPotionSlot", LCB_GUI_OPTIONS_SC, "InputBoxTemplate");
	LCB_ManaPotionSlot:SetPoint("TOPLEFT", LCB_HealthPotionSlot, "TOPLEFT", 0, -30);
	LCB_GUI_SetEditBoxDetails(LCB_ManaPotionSlot);
	LCB_GUI_AddDescLeft(LCB_ManaPotionSlot, "Mana Potion Button Slot -");

--[[
	Need to put a header in here somehow
	
	This handles the stuff at the end of the screen.
	
	]]

	-- Save Button
	local LCB_GUI_SaveButton = CreateFrame("Button", "LCB_GUI_SaveButton", LCB_GUI_OPTIONS_SC, "UIPanelButtonTemplate");
	LCB_GUI_SaveButton:SetWidth(150);
	LCB_GUI_SaveButton:SetHeight(22);
	LCB_GUI_SaveButton:SetText(SAVE);
	LCB_GUI_SaveButton:RegisterForClicks("LeftButtonUp");
	LCB_GUI_SaveButton:ClearAllPoints();
	LCB_GUI_SaveButton:SetPoint("TOPLEFT", LCB_GUI_OPTIONS_SC, "BOTTOMLEFT", 20, 40);
	LCB_GUI_SaveButton:SetScript("OnClick", LCB_GUI_SaveButtonClick);

	-- Defaults Button
	local LCB_GUI_DefaultsButton = CreateFrame("Button", "LCB_GUI_DefaultsButton", LCB_GUI_OPTIONS_SC, "UIPanelButtonTemplate");
	LCB_GUI_DefaultsButton:SetWidth(150);
	LCB_GUI_DefaultsButton:SetHeight(22);
	LCB_GUI_DefaultsButton:SetText("Defaults");
	LCB_GUI_DefaultsButton:RegisterForClicks("LeftButtonUp");
	LCB_GUI_DefaultsButton:ClearAllPoints();
	LCB_GUI_DefaultsButton:SetPoint("TOPLEFT", LCB_GUI_SaveButton, "TOPLEFT", 160, 0);
	LCB_GUI_DefaultsButton:SetScript("OnClick", LCB_GUI_DefaultsButtonClick);

	-- Defaults Button - No Mana
	local LCB_GUI_DefaultsNMButton = CreateFrame("Button", "LCB_GUI_DefaultsNMButton", LCB_GUI_OPTIONS_SC, "UIPanelButtonTemplate");
	LCB_GUI_DefaultsNMButton:SetWidth(150);
	LCB_GUI_DefaultsNMButton:SetHeight(22);
	LCB_GUI_DefaultsNMButton:SetText("Defaults - No Mana");
	LCB_GUI_DefaultsNMButton:RegisterForClicks("LeftButtonUp");
	LCB_GUI_DefaultsNMButton:ClearAllPoints();
	LCB_GUI_DefaultsNMButton:SetPoint("TOPLEFT", LCB_GUI_DefaultsButton, "TOPLEFT", 160, 0);
	LCB_GUI_DefaultsNMButton:SetScript("OnClick", LCB_GUI_DefaultsNMButtonClick);

	-- Set an "OnShow" script for the GUI screen to fill in the config data.
	LCB_GUI:SetScript("OnShow", LCB_GUI_FillData);
end

